package gold.digger;

/**
 * Created by fanzhenyu02 on 2020/3/1.
 */
public class LC45 {

    public int jump(int[] nums) {
        int n = nums.length - 1, step = 0;
        while (n != 0) {
            for (int i = 0; i < n; i++) {
                if (nums[i] + i >= n) {
                    n = i;
                    step++;
                    break;
                }
            }
        }

        return step;
    }


    public int jumpRecu(int[] nums) {
        return jumpWithRetry(nums, 0, 0);
    }

    public int jumpWithRetry(int[] nums, int start, int step) {
        if (start >= nums.length - 1) {
            return step;
        }

        int curMinStep = Integer.MAX_VALUE;
        for (int i = 1; i <= nums[start]; i++) {
            int curStep = jumpWithRetry(nums, start + i, step + 1);
            if (curMinStep > curStep) {
                curMinStep = curStep;
            }
        }

        return curMinStep;
    }

    public static void main(String[] args) {
        LC45 an = new LC45();
        int[] road = {2, 3, 1, 1, 4};
        System.out.println(an.jump(road));
    }
}
